layui.use(['layer', 'form', 'table', 'admin',  'laydate','ax',"upload"], function () {
    //var $ = layui.$;
    var $= layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var upload = layui.upload;
    var laydate = layui.laydate;

    /**
     * 系统管理--评价管理
     */
    var OBJ = {
        tableId: "paymentTable",    //表格id
        condition: {
            meterNo: "",
            shopName:""
        }
    };

    /**
     * 初始化表格的列
     */
    OBJ.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'meterNo', sort: true, title: '表编号'},
            {field: 'shopName', sort: true, title: '商户名称'},
            {field: 'paymentName', sort: true, title: '缴费类型'},
            {field: 'totlePrice', sort: true, title: '金额(元)'},
            {field: 'createTime', sort: true, title: '出账时间',width:200},
            {field: 'payStatusName', sort: true, title: '状态'},
            {field: 'payTime', sort: true, title: '支付时间',width:200},
            {align: 'left', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };
    //导入 用layui upload插件
    var upload = layui.upload;

    //执行实例
    var uploadInst = upload.render({
        elem: '#importData' //绑定元素
        ,url: Feng.ctxPath + '/payment/importData' //上传接口
        ,done: function(res){
            //上传完毕回调
            alert(1);
        }
        ,error: function(){
            //请求异常回调
            alert(2);
        }
    });
    /**
     * 点击查询按钮
     */
    OBJ.search = function () {
        var queryData = {};
        queryData['meterNo'] = $("#meterNo").val().replace(/\s+/g,"");
        queryData['shopName'] = $("#shopName").val().replace(/\s+/g,"");
        queryData['createTime'] = $("#createTime").val();
        queryData['payTime'] = $("#payTime").val();
        queryData['createMonthTime'] = $("#createMonthTime").val();
        queryData['payStatus'] = $("#payStatus").val();
        queryData['paymentType'] = $("#paymentType").val();
        table.reload(OBJ.tableId,{
            page:{
                curr:1
            },
            where: queryData
        })
    };
    //渲染时间选择框
    laydate.render({
        elem: '#createTime',
        range: true,
        max: Feng.currentDate()
    });
    //渲染时间选择框
    laydate.render({
        elem: '#payTime',
        range: true,
        max: Feng.currentDate()
    });
    //渲染时间选择框
    //年月选择器
    laydate.render({
        elem: '#createMonthTime'
        ,type: 'month'
    });
    /**
     * 点击详情
     *
     * @param data 点击按钮时候的行数据
     */
    OBJ.onDetailPayment = function (data) {
        admin.putTempData('formOk', false);
        var payStatusName = encodeURI(data.payStatusName);
        top.layui.admin.open({
            type: 2,
            area: '1000px',
            title: '缴费详情 &nbsp(表编号:'+data.meterNo+')',
            content: Feng.ctxPath + '/payment/payment_detail?id=' + data.id+'&payStatusName='+payStatusName,
            end: function () {
                admin.getTempData('formOk') && table.reload(OBJ.tableId);
            },
            btn: ['返回'],
            btnAlign: 'c'
        });
    };


    /**
     * 导出excel按钮
     */
    OBJ.exportExcel = function () {
        var checkRows = table.checkStatus(OBJ.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OBJ.tableId,
        url: Feng.ctxPath + '/payment/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: OBJ.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OBJ.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        OBJ.openAddRole();
    });
    // 导出excel
    $('#btnExp').click(function () {
        OBJ.exportExcel();
    });
    // 导入excel
    $('#importData').click(function () {
        OBJ.importData();
    });


    // 工具条点击事件
    table.on('tool(' + OBJ.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'dispatch') {
            OBJ.onDispatchTicket(data);
        } else if (layEvent === 'delete') {
            OBJ.onDeleteTicket(data);
        } else if (layEvent === 'detail') {
            OBJ.onDetailPayment(data);
        }
    });
});
